<?php

function create_Permission($uid)
{
    global $_GCONFIG;
    $group = $_GCONFIG["permission"]["defaultGroup"];
    T("permission_user")->create($uid, array(
        $group
    ));
}

function fetch_groups($uid)
{
    $back = T("permission_user")->fetch_all_by_uid($uid);
    return array(
        "uid" => $back["uid"],
        "groups" => json_decode($back["groups"]),
        "admin_groups" => json_decode($back["admin_groups"]),
        "extra_permissions" => json_decode($back["extra_permissions"]),
        "extra_ban" => json_decode($back["extra_ban"])
    );
}

function fetch_permissions($groupname)
{
    $back = T("permission_group")->fetch_permissions_by_groupname($groupname);
    $o_extends = json_decode($back["extends"]);
    $extends = $o_extends == null ? array() : $o_extends;
    $permissions = json_decode($back["permissions"]);
    if (! empty($extends)) {
        foreach ($extends as $value) {
            $backc = fetch_permissions($value);
            $permissions = array_merge($permissions, $backc["permissions"]);
            if (! empty($backc["extends"])) {
                $extends = array_merge($extends, $backc["extends"]);
            } else {}
        }
    }
    return array(
        "name" => $groupname,
        "permissions" => $permissions,
        "extends" => $extends
    );
}

function loadPermission($uid)
{
    global $_G;
    $groups = fetch_groups($uid);
    $perms = array();
    $perms["extra_permissions"] = $groups["extra_permissions"] == null ? array() : $groups["extra_permissions"];
    $perms["extra_ban"] = $groups["extra_ban"] == null ? array() : $groups["extra_ban"];
    if (! empty($groups["groups"])) {
        foreach ($groups["groups"] as $value) {
            $perms["groups"][$value] = fetch_permissions($value);
        }
    } else
        $perms["groups"] = array();

    if (! empty($groups["admin_groups"])) {
        foreach ($groups["admin_groups"] as $value) {
            $perms["admin_groups"][$value] = fetch_permissions($value);
        }
    } else
        $perms["admin_groups"] = array();
    $perms["isAdmin"] = (! empty($perms["admin_groups"]));
    $_G["permissions"] = $perms;
}

function hasPermission($perm, $type = "groups", $groupname = "")
{
    global $_G;
    if ($groupname) {
        return in_array("perm", $_G["permissions"][$type][$groupname]);
    }
    foreach ($_G["permissions"][$type] as $v) {
        if (in_array($perm, $v["permissions"])) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为超级访问者，常见关系诸如总管理员对所有用户，家长对子女，班主任对相关班级学生
 *
 * @param int $uid
 * @param int $targetid
 */
function isSuperVisitor($uid, $targetid)
{
    if (is_Master($targetid, $uid)) {
        return true;
    }
    if (isChild($targetid, $uid)) {
        return true;
    }
    return false;
}

function getPermission($pname)
{
    return T("permission_list")->fetch_by_name($pname);
}

function getGroup($gname)
{
    $back = T("permission_group")->fetch_all_by_groupname($gname);
    if (! $back)
        return T("permission_group")->fetch_all_by_gid($gname);
    return $back;
}
?>